package com.ht.daoimpl;

import java.util.List;
import com.ht.dao.PersonDao;
import com.ht.model.Person;

public class PersonDaoImpl extends HibernateDao implements PersonDao {


	public int insertPerson(Person person) {
		int result = super.insert(person);
		return result;
	}
	

	public int deletePerson(Person person) {
		int result = super.delete(person);
		return result;
	}

	public int updatePerson(Person person) {
		int result = this.update(person);
		return result;
	}


	
	public List<Person> selectByCon(Person person,int curPage ,int pageSize) {
		String hql = "select app from Person app where 1=1 ";
		if (person != null) {
			if (person.getId()!= null) {
				hql = hql + " and Id like '%" + person.getId() + "%' ";
			}
			if (person.getName()!= null) {
				hql = hql + " and name like '%" + person.getName() + "%' ";
			}
			if (person.getDept()!= null) {
				hql = hql + " and dept like '%" + person.getDept() + "%' ";
			}
		}
		
		List<Person> allemps = super.exeQueryByPage(hql, null,curPage,pageSize);
		
		return allemps;
	}
	
	
	public Person getPersonByName(String name) {
		String hql = "select app from Person app where name=?";
		String[] args = new String[1]; 
		args[0] = name ;
		Object o =  super.getUniqueObject(hql, args);
		Person person = (o!=null) ? (Person)o :null;
		return person;
	}	
	
	public Person getPersonByDept(String dept) {
		String hql = "select app from Person app where dept=?";
		String[] args = new String[1]; 
		args[0] = dept ;
		Object o =  super.getUniqueObject(hql, args);
		Person person = (o!=null) ? (Person)o :null;
		return person;
	}	
	
	public Person getPersonById(Person person) {
		Object o = super.get(Person.class, person.getId());
		Person sa = (o!=null) ? (Person)o : null ;
		return sa;
	}
	
	public int getRecordCount(Person person) {
		String hql = "select count(*) from Person where 1=1 ";
		if (person != null) {
			if (person.getId() != null) {
				hql = hql + " and id like '%" + person.getId() + "%' ";
			}
			if (person.getName() != null) {
				hql = hql + " and name like '%" + person.getName() + "%' ";
			}
			if (person.getDept() != null) {
				hql = hql + " and dept like '%" + person.getDept() + "%' ";
			}


		}
		Object o = super.getUniqueObject(hql, null);
        int result = Integer.valueOf(o.toString());
		return result;

	}





}
